//
//  FeedbackVC.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/13.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit
import IQKeyboardManagerSwift

class FeedbackVC: BaseVC {

    @IBOutlet weak var tf: IQTextView!
    @IBOutlet weak var describeLabel: UILabel!
    
    
    var pageType:Int = 0// 0建议反馈，1投诉
    
    var bid:Int?
    var key:String?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = pageType == 0 ? "反馈建议" : "投诉"
        
        if pageType == 1 {
            
            describeLabel.text = "请详细描述投诉事件的过程"
            self.tf.placeholder = "请详细描述投诉事件的过程"
            
        }
    }
    
    @IBAction func commitAction(_ sender: Any) {
        
        if pageType == 1 {
            
            guard let text = tf.text, text.count >= 10 else {
                
                self.showToast("最少输入10个字符")
                
                return
            }
            
            NetAPI.complaints(bid: bid!, remark: text, key: key!).request {
                
                [weak self] response, errmsg in
                guard let `self` = self,let res = response as? [String:String] else { return }
                
                if let msg = errmsg  {
                    
                    self.showToast(msg)
                } else {
                    
                    self.showToast(res["result"])
                    
                    guard let vcs = self.navigationController?.viewControllers else {
                        
                        self.dismiss(animated: true, completion: nil)
                        
                        return
                    }
                    
                    if vcs.count > 2 {
                        
                        self.navigationController?.popToViewController(vcs[vcs.count - 3], animated: true)
                    } else {
                        
                        self.navigationController?.popViewController(animated: true)
                    }
                }
                
            }

            return
        }
        
        guard let text = tf.text, text.count >= 5 else {
            
            self.showToast("反馈内容最少5个字")
            
            return
        }
        
        NetAPI.suggest(remark: text).request {
            
            [weak self] response, errmsg in
            guard let `self` = self else { return }
            
            if let msg = errmsg  {
                
                self.showToast(msg)
            } else {
                
                self.showToast("感谢您的反馈，我们会尽快处理！")
                self.navigationController?.popViewController(animated: true)
            }
            
        }
        
    }
}
